<?php

namespace app\admin\model;

use think\Model;

class User extends Model
{
    // 表名
    protected $name = 'user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'reg_time_text',
        'healthy_text',
        'black_text',
        'last_login_time_text',
        'role_text',
        'delete_time_text'
    ];
    

    
    public function getHealthyList()
    {
        return ['0' => __('Healthy 0'),'1' => __('Healthy 1'),'2' => __('Healthy 2'),'3' => __('Healthy 3')];
    }     

    public function getBlackList()
    {
        return ['0' => __('Black 0'),'1' => __('Black 1')];
    }     

    public function getRoleList()
    {
        return ['grouper' => __('Grouper'),'user' => __('User')];
    }     


    public function getRegTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reg_time']) ? $data['reg_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getHealthyTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['healthy']) ? $data['healthy'] : '');
        $list = $this->getHealthyList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBlackTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['black']) ? $data['black'] : '');
        $list = $this->getBlackList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLastLoginTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['last_login_time']) ? $data['last_login_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRoleTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['role']) ? $data['role'] : '');
        $list = $this->getRoleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setRegTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setLastLoginTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function userAttribute(){
        return $this->hasOne('app\common\model\UserAttribute','attr_id');
    }



}
